﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
/*
                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\  =  /O
               ____/`---'\____
             .'  \\|     |//  `.
            /  \\|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . __
      ."" '<  `.___\_<|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
       		   佛祖保佑       永无BUG
*/


namespace Mesgesssystem
{
    class DBHelper
    {
        string SqlText = "Data Source=FDC;Initial Catalog=学生信息管理系统;Integrated Security=True;User ID=sa;Password=fdcc199606";//定义数据库连接字符串
        string SqlText1 = "Data Source=FDC;Initial Catalog=学生信息管理系统;Integrated Security=True;User ID=sa;Password=fdcc199606";
        DataSet data; //备份恢复需要使用master数据库，定义master数据库连接字符串
        public DBHelper()
        {

        }
        public DataSet DBselect(string commandText, params SqlParameter[] parameters)
        {
            if (SqlText == null)
                return null;
            SqlConnection con = new SqlConnection(SqlText);
            try
            {
                var cmd = con.CreateCommand();
                cmd.CommandText = commandText;

                if (parameters != null)
                {
                    cmd.Parameters.AddRange(parameters);
                }
                SqlDataAdapter adpt = new SqlDataAdapter(cmd);
                data = new DataSet();
                adpt.Fill(data);
            }
            catch //(Exception ex)
            {

            }
            return data;
        }
        public string DBinsert1(string commandText, params SqlParameter[] parameters)
        {
            if (SqlText1 == null)
                return "请设置数据库连接字符串";
            using (SqlConnection con = new SqlConnection(SqlText))
            {
                try
                {
                    using (var cmd = con.CreateCommand())
                    {
                        cmd.CommandText = commandText;
                        if (parameters != null)
                        {
                            cmd.Parameters.AddRange(parameters);
                        }
                        con.Open();
                        cmd.ExecuteNonQuery();
                        cmd.Parameters.Clear();
                    }
                    return "成功";
                }
                catch (Exception ex)
                {

                    return ex.Message.ToString();
                }
            }
        }
        public string DBinsert(string commandText, params SqlParameter[] parametets)
        {
            if (SqlText == null)
                return "请设置数据库连接字符串";
            using (SqlConnection con = new SqlConnection(SqlText))
            {
                try
                {
                    using (var cmd = con.CreateCommand())
                    {
                        cmd.CommandText = commandText;
                        if (parametets != null)
                        {
                            cmd.Parameters.AddRange(parametets);
                        }
                        con.Open();
                        cmd.ExecuteNonQuery();
                        cmd.Parameters.Clear();
                    }
                    return "成功";
                }
                catch (Exception d)
                {
                    return d.Message.ToString();
                }
            }
        }
    }
}
